import React from 'react';
import { connect } from 'dva';
import { Link, browserHistory } from 'dva/router';
import { Table, Row, Col, Form, Input, Radio, Select, Button, message, Popconfirm } from 'antd';
const R = require('ramda');
import Constants from '../../../utils/Constants';
import { downloadFile } from '../../../utils/commons'
const FormItem = Form.Item;
export default connect(({ vipManage }) => ({ vipManage }))(
    Form.create()(class financeRecord extends React.Component {
        constructor(props) {
            super(props);
        }

        componentDidMount() {
            this.props.dispatch({ type: 'vipManage/queryFinanceRecords' });//财税记录查询
        }

        handleSearch = () => {
            let values = this.props.form.getFieldsValue();
            let newPage = this.props.vipManage.fPage;
            newPage.page = 1;
            this.props.dispatch({
                type: 'vipManage/queryFinanceRecords',
                payload: {
                    cstName: values.cstName,
                    page: newPage
                }
            });
        };

        columns = [
            { title: '客户名称', dataIndex: 'cstName', key: 'cstName' },
            {
                title: '财税附件', render(record) {
                    let fileName = R.isNil(record.url) || R.isEmpty(record.url) ? "无" : record.url.substring(record.url.lastIndexOf('/') + 1, record.url.length);
                    return <a href="#" onClick={() => downloadFile(Constants.FILE_MODULE[1], record.url)}> {fileName}</a>
                }
            },
            { title: '创建人', dataIndex: 'creatorName', key: 'creatorName' },
            { title: '创建时间', dataIndex: 'createTime', key: 'createTime' }
        ];

        render() {
            let dataSource = R.isNil(this.props.vipManage.fList) ? {} : this.props.vipManage.fList;
            let formItemLayout = { labelCol: { span: 6 }, wrapperCol: { span: 18 } };
            const { getFieldDecorator } = this.props.form;
            let values = this.props.form.getFieldsValue();
            let pagination = {
                showTotal: total => `共 ${total} 条数据`,
                current: this.props.vipManage.fPage.page,
                pageSize: this.props.vipManage.fPage.size,
                total: this.props.vipManage.fPage.totalRow,
                showSizeChanger: true,
                onShowSizeChange: (current, size) => {
                    let newPage = this.props.vipManage.fPage;
                    newPage.size = size;
                    newPage.page = 1;
                    this.props.dispatch({
                        type: 'vipManage/queryFinanceRecords',
                        payload: {
                            cstName: values.cstName,
                            page: newPage
                        }
                    });
                },
                onChange: (pageNumber, pageSize) => {
                    let newPage = this.props.vipManage.fPage;
                    newPage.page = pageNumber;
                    this.props.dispatch({
                        type: 'vipManage/queryFinanceRecords',
                        payload: {
                            cstName: values.cstName,
                            page: newPage
                        }
                    });
                }
            }

            return (
                <div>
                    <Row>
                        <Col span={6}><FormItem {...formItemLayout} label={'客户名称'}>{getFieldDecorator('cstName')(<Input />)}</FormItem></Col>
                        <Col span={6} style={{ float: 'right' }}>
                            <Button type="primary" icon="search" style={{ marginRight: 5 }} onClick={this.handleSearch.bind(this)}>查询</Button>
                        </Col>
                    </Row>
                    <Table rowKey={record => record.id} dataSource={dataSource} columns={this.columns} bordered={true} size={'middle'} pagination={pagination} />
                </div>
            )
        }
    })
)
